class_name BuffAntiGravityAttack
extends BaseBuff


@export var countdown_time: float = 1.0
@onready var countdown_label: Label = $Countdown

var attack: float = 0
var critical_rate: float = 0
var critical_bonus: float = 0
var knockback: float = 0
var affect_vertical_velocity: float = 0
var cam_shake_strength: float = 0
var cam_shake_duration: float = 0
var is_enemy: bool = false


func _process(delta: float) -> void:
	countdown_time -= delta
	
	if countdown_time <= 0:
		countdown_time = 0
		countdown_label.text = String.num(countdown_time, 1)
		
		gravity_attack()
		queue_free()
		return
		
	countdown_label.text = String.num(countdown_time, 1)
	
	
func gravity_attack():
	var inst = (ResourceManager.get_resource("gravity_attack") as PackedScene).instantiate() as GravityAttack
	holder.level.particles_parent.add_child(inst)
	inst.sender = sender
	inst.attack = attack
	inst.critical_rate = critical_rate
	inst.critical_bonus = critical_bonus
	inst.knockback = knockback
	inst.affect_vertical_velocity = affect_vertical_velocity
	inst.cam_shake_duration = cam_shake_duration
	inst.cam_shake_strength = cam_shake_strength
	inst.set_is_enemy(is_enemy)
	
	inst.global_position = holder.global_position + Vector2.DOWN * 150
	var target_pos = holder.global_position + Vector2.UP * 150
	var hit_ground = false
	
	#var result = GlobalUtils.raycast_to(holder.global_position, holder.global_position + Vector2.DOWN * 150, 1)
	#if result:
		#target_pos = result["position"]
		#hit_ground = true
	
	inst.start_attack(target_pos, hit_ground)
	
	
